// JavaScript Document

var ItemLoadType=null;
var ItemInUse = null;

function callPackage(tar)
{
	var i=0;
	while(true)
	{
		try
		{
		$('CIM'+i).className='characterItemType';
		}catch(e){break;}
		i++;
	}
	tar.className = 'characterItemType_selected';
	
	switch(tar.id)
	{
		case 'CIM0':
			
			ItemLoadType = 'item';
			callback = showItem;
			break;
		
		case 'CIM1':
			
			ItemLoadType = 'equ';
			callback = showEquipment;
			break;
		
		default:
			
			break;
	}
	
	new Ajax.Request(
					 'd5power.php', {method: 'get', parameters: 'action=getItem&type='+ItemLoadType, onComplete:
		function(result)
		{
			if(result.responseText == '') return;
			var serverdata = result.responseText=='NONE' ? 'NONE' : eval("(" + result.responseText + ")");
			callback(serverdata);
		}
	}
	);
}


function showItem(data)
{
	$('equipmentBox').style.display='none'; // 不显示装备
	var result = '';
	for(var i=0;i<30;i++)
	{
		item_img = data[i]==undefined ? 'none' : 'url(../pictures/item/'+data[i].id+'.gif)';
		item_info = data[i]==undefined ? '' : data[i].name+'&#10;'+data[i].info;
		item_num = data[i]==undefined ? '' : data[i].num;
		item_id = data[i]==undefined ? 'empty'+i : data[i].id;
		item_function = data[i]==undefined ? '' : 'useItem('+item_id+')';
		
		result+='<div id="item_'+item_id+'" class="ItemBox" ondblclick="'+item_function+'" style="background-image:'+item_img+'" title="'+item_info+'">';
        result+='<div id="itemNumber_'+item_id+'" class="ItemNumber">'+item_num+'</div>';
        result+='</div>';		
	}
	$('characterItemBox').style.display='';
	$('characterItemBox').innerHTML = result;
}

function useItem(item_id)
{
	if(ItemInUse!=null)
	{
		gameConsole('您正在使用其他物品，请稍侯');
		return;
	}
	
	try{
		if(rival!=null)
		{
			gameConsole('您正在战斗中，请使用战斗物品菜单');
			return;
		}
	}catch(e){}
	
	
	ItemInUse = item_id;
	
	new Ajax.Request(
					 'd5power.php', {method: 'get', parameters: 'action=useItem&item_id='+item_id, onComplete:
		function(result)
		{
			ItemInUse=null;
			if(result.responseText == '') return;
			var serverdata = eval("(" + result.responseText + ")");
			var haschange = false;
			
			if(serverdata.hp>0)
			{
				myCharacter.hp=serverdata.hp;
				haschange = true;
			}
			
			if(serverdata.str>0)
			{
				myCharacter.str=serverdata.str;
				haschange = true;
			}
			
			if(serverdata.dex>0)
			{
				myCharacter.dex=serverdata.dex;
				haschange = true;
			}
			
			if(serverdata.int>0)
			{
				myCharacter.int=serverdata.int;
				haschange = true;
			}
			
			if(serverdata.vit>0)
			{
				myCharacter.vit=serverdata.vit;
				haschange = true;
			}
			
			if(serverdata.agi>0)
			{
				myCharacter.agi=serverdata.agi;
				haschange = true;
			}
			
			if(serverdata.atk>0)
			{
				myCharacter.atk=serverdata.atk;
				haschange = true;
			}
			
			if(serverdata.def>0)
			{
				myCharacter.def=serverdata.def;
				haschange = true;
			}
			
			if(haschange)
			{
				myCharacter.setupUserinfo();
				callPackage($('CIM0'));
			}
		}
	}
	);
}


// 增加一件新的物品
function addNewItem(item_id)
{
	//alert(item_id);
}

function showEquipment(data)
{
	$('characterItemBox').style.display='none';
	$('equipmentBox').style.display='';
	if(data=='NONE') return;
	
	var i=0;
	while(true)
	{
		try
		{
			if(data[i]==undefined) break;
			$(data[i]._where).style.backgroundImage='url(../pictures/item/'+data[i].item_id+'.gif)';
			info = data[i]._info;
			info = info.split('&#10;');
			var _info = '';
			for(m=0;m<info.length;m++)
			{
				_info+=info[m];
				if(m==info.length-1) break;
				_info+='\n';
			}
			$(data[i]._where).title=_info;
			i++;
		}catch(e){
			break;
		}
	}	
}